package com.study.zouchao.superproject.web;

import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.study.zouchao.superproject.R;
import com.study.zouchao.superproject.util.EventBusUtils;
import com.study.zouchao.superproject.util.StringUtils;

import butterknife.BindView;
import butterknife.ButterKnife;

public class WebActivity extends AppCompatActivity {
    public static final String KEY_TARGET_URL = "KEY_TARGET_URL";
    private String mTargetUrl;
    @BindView(R.id.id_wv)
    WebView mWv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web);
        ButterKnife.bind(this);
        init();
    }

    private void init() {
        mWv.getSettings().setJavaScriptEnabled(true);
        mWv.getSettings().setBlockNetworkImage(false);
        mWv.setWebViewClient(new WebViewClient());

        Intent intent = this.getIntent();
        mTargetUrl = intent.getStringExtra(KEY_TARGET_URL);
        load();
    }

    private void load() {
        if (StringUtils.isEmpty(mTargetUrl))    return;
        mWv.loadUrl(mTargetUrl);
    }
    public static void actionStartWebActivity(Context context, String targetUrl) {
        Intent intent = new Intent(context, WebActivity.class);
        intent.putExtra(KEY_TARGET_URL, targetUrl);
        context.startActivity(intent);
    }

    //设置回退
    //覆盖Activity类的onKeyDown(int keyCoder,KeyEvent event)方法
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && mWv.canGoBack()) {
            mWv.goBack(); //goBack()表示返回WebView的上一页面
            return true;
        }
        finish();//结束退出程序
        return false;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
